package com.kafka.demoka.common;

import com.alibaba.fastjson.JSONObject;
import com.kafka.demoka.entity.User;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class kafkaConsumer {
    @KafkaListener(topics = "user")
    public void consumer(ConsumerRecord consumerRecord) {
        Optional<Object> kafkaMassage = Optional.ofNullable(consumerRecord.value());
        if (kafkaMassage.isPresent()) {
            Object o = kafkaMassage.get();
            User person = JSONObject.parseObject(o.toString(), User.class);
            // 相关的业务代码操作
            System.out.println(person.getName());
        }
    }
}
